//
//  ProductCellV1.m
//  danpin
//
//  Created by chuangjia on 27/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "ProductCellV1.h"

#import "ZUIImageView.h"

@interface ProductCellV1 ()
{
    
}
@property(nonatomic,strong)UIImageView *imagesView;
@property(nonatomic,strong)RTLabel *titleLabel;
@property(nonatomic,strong)RTLabel *brandLabel;

@property(nonatomic,strong)RTLabel *introLabel;

@property(nonatomic,strong)UILabel *priceLabel;
@property(nonatomic,strong)UILabel *picLabel;
@property(nonatomic,strong)UIImageView *shaidanView;
@property(nonatomic,strong)UIImageView *lineView;
@property(nonatomic,strong)UIButton *topBtn;
@property(nonatomic,strong)UIButton *delBtn;
@end
@implementation ProductCellV1

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor whiteColor];
        _imagesView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _imagesView.backgroundColor = UIColorFromRGB(0xffffff);
        _imagesView.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_imagesView];
        
        _topBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _topBtn.backgroundColor =[UIColor clearColor];
        [_topBtn setImage:[UIImage imageNamed:@"ding_top.png"] forState:UIControlStateNormal];//给button添加image
        _topBtn.imageEdgeInsets = UIEdgeInsetsMake(0,10,21,_topBtn.titleLabel.bounds.size.width);
        _topBtn.titleLabel.font = FONT(16);//title字体大小
        _topBtn.titleLabel.textAlignment = NSTextAlignmentCenter;//设置title的字体居中
        [_topBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];//设置title在一般情况下为白色字体
        
        _topBtn.titleEdgeInsets = UIEdgeInsetsMake(17, -_topBtn.titleLabel.bounds.size.width-16, 0, 0);//设置title在button上的位置（上top，左left，下bottom，右right）
        [_topBtn addTarget:self action:@selector(topAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_topBtn];
        //
        _brandLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*2,0)];
        _brandLabel.backgroundColor = [UIColor clearColor];
        _brandLabel.textColor = UIColorFromRGB(0x891404);
        _brandLabel.textAlignment = NSTextAlignmentLeft;
        _brandLabel.font = [UIFont boldSystemFontOfSize:14];
        _brandLabel.lineSpacing =4;
        [self.contentView addSubview:_brandLabel];
        
        _titleLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*2,0)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = [UIColor darkGrayColor];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _titleLabel.lineSpacing =4;
        [self.contentView addSubview:_titleLabel];
        
        _introLabel=[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*2,0)];
        _introLabel.backgroundColor = [UIColor clearColor];
        _introLabel.textColor = UIColorFromRGB(0xa5a5a5);
        _introLabel.textAlignment = NSTextAlignmentLeft;
        _introLabel.font = FONT(14);
        _introLabel.lineSpacing =4;
        [self.contentView addSubview:_introLabel];
        
        _priceLabel =[[UILabel alloc] initWithFrame:CGRectZero];
        _priceLabel.backgroundColor = [UIColor clearColor];
        _priceLabel.textColor = [UIColor grayColor];
        _priceLabel.textAlignment = NSTextAlignmentLeft;
        _priceLabel.font = FONT(12);
        _priceLabel.numberOfLines=0;
        [self.contentView addSubview:_priceLabel];
        //
        _picLabel =[[UILabel alloc] initWithFrame:CGRectZero];
        _picLabel.backgroundColor = [UIColor clearColor];
        _picLabel.textColor = [UIColor lightGrayColor];
        _picLabel.textAlignment = NSTextAlignmentRight;
        _picLabel.font = FONT(12);
        [self.contentView addSubview:_picLabel];
        //
        _shaidanView =[[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_shaidanView];
        //
        _delBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _delBtn.titleLabel.font =[UIFont boldSystemFontOfSize:20];
        [_delBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_delBtn setTitle:@"..." forState:UIControlStateNormal];
        [_delBtn addTarget:self action:@selector(delRankAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_delBtn];
        //
        _lineView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _lineView.image =[UIImage imageNamed:@"line_xu.png"];
        [self.contentView addSubview:_lineView];
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock=nil;
    self.data=nil;
    self.key=nil;
    self.other=nil;
    self.indexPath=nil;
    
    self.imagesView.image=nil;
    [self.imageView removeFromSuperview];
    self.imagesView=nil;
    self.topBtn =nil;
    [self.titleLabel removeFromSuperview];
    self.titleLabel=nil;
    [self.brandLabel removeFromSuperview];
    self.brandLabel=nil;
    [self.introLabel removeFromSuperview];
    self.introLabel=nil;
    [self.priceLabel removeFromSuperview];
    self.priceLabel=nil;
    [self.picLabel removeFromSuperview];
    self.picLabel =nil;
    [self.shaidanView removeFromSuperview];
    self.shaidanView=nil;
    self.delBtn =nil;
    [self.lineView removeFromSuperview];
    self.lineView=nil;

    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    _priceLabel.frame=CGRectMake(DEVICE_WIDTH/2+PHOTO_FRAME_WIDTH,self.bounds.size.height -MORE_BUTTON_HEIGHT, 150, 20);
    
    _delBtn.frame=CGRectMake(DEVICE_WIDTH - 50 -PHOTO_FRAME_WIDTH, self.bounds.size.height -MORE_BUTTON_HEIGHT-PHOTO_FRAME_WIDTH, 50, MORE_BUTTON_HEIGHT);
    
    self.lineView.frame =CGRectMake(0, self.bounds.size.height-1, DEVICE_WIDTH, 1);
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model other:(id)other key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.data=model;
    self.other=other;
    self.key=key;
    self.indexPath=indexPath;
    [self showInfo:model key:key indexPath:indexPath];
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.indexPath=indexPath;
    self.key = key;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    
    CGFloat fTop =PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING;
    self.topBtn.frame =CGRectMake(PHOTO_FRAME_WIDTH, fTop, PHOTO_FRAME_WIDTH*5, PHOTO_FRAME_WIDTH*5);
    NSString *topNum =[NSString stringWithFormat:@"%@",[dict objectForKey:@"ware_push_top"]];
    int hadTop =[[dict objectForKey:@"had_ware_push_top"] intValue];
    if (hadTop) {
        [_topBtn setTitleColor:UIColorFromRGB(0xd00202) forState:UIControlStateNormal];
    }else{
        [_topBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    }
    [self.topBtn setTitle:topNum forState:UIControlStateNormal];
    //
    
    NSString * strURL = [NSString stringWithFormat:@"%@", [dict objectForKey:@"largePicURL"]];
    CGFloat H =0;
    if (strURL && strURL.length>0) {
        BOOL need = false;
        CGFloat w =[[dict objectForKey:@"largePicWidth"] floatValue];
        CGFloat h =[[dict objectForKey:@"largePicHeight"] floatValue];
        if (w>0 && h>0) {
            CGFloat fullW =DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*8;
            CGFloat ratio = fullW / w;
            H = ratio * h;
        }
        else
        {
            H = DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*8;
            
            need = YES;
        }
        _imagesView.frame =CGRectMake(PHOTO_FRAME_WIDTH*7,fTop, DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*8, H);
        _imagesView.backgroundColor =[UIColor clearColor];
        __block UIActivityIndicatorView *activityIndicator=nil;
        __weak UIImageView *weakImageView = _imagesView;
        
        [weakImageView sd_setImageWithURL:[NSURL URLWithString:strURL] placeholderImage:nil options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            if (!activityIndicator)
            {
                [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                activityIndicator.backgroundColor=[UIColor clearColor];
                activityIndicator.center = CGPointMake(weakImageView.center.x/2, weakImageView.center.y);
                [activityIndicator startAnimating];
            }
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (error) {
                image=[UIImage imageNamed:@"listcellimage.png"];
                weakImageView.image=[UIImage imageNamed:@"listcellimage.png"];
            }
            
            CGFloat fImageSize = DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*8;
            CGFloat w =image.size.width;
            CGFloat h =image.size.height;
            CGFloat ratio = fImageSize / w;
            CGFloat x=0;
            if(ratio <= 1)
            {
                w =fImageSize;
                h = h*ratio;
            }
            if(need || ratio > 1)
            {
                if(w > h)
                {
                    
                    CGFloat r = h / w;
                    if(w > fImageSize)
                        w = fImageSize;
                    
                    h = w * r;
                }
                else
                {
                    CGFloat r = w / h;
                    if(h > fImageSize)
                        h = fImageSize;
                    
                    w = h * r;
                }
//                x = (fImageSize-w)/2;
            }
            
            weakImageView.frame = CGRectMake(PHOTO_FRAME_WIDTH*7,fTop, w, h);
            int is_shaidan =[[dict objectForKey:@"is_shaidan"] intValue];
            if (is_shaidan>0)
            {
                _shaidanView.frame =CGRectMake(weakImageView.frame.origin.x+weakImageView.frame.size.width-32-PHOTO_FRAME_WIDTH,weakImageView.frame.origin.y+weakImageView.frame.size.height- 16 - PHOTO_TIME_PADDING, 32, 16);
                NSString *siconView =[NSString stringWithFormat:@"%@",[dict objectForKey:@"icon_url"]];
                [_shaidanView sd_setImageWithURL:[NSURL URLWithString:siconView]];
                
            }
            [weakImageView.layer addAnimation:[CATransition animation] forKey:kCATransition];
            [activityIndicator stopAnimating];
            [activityIndicator removeFromSuperview];
            activityIndicator = nil;
        }];
        
    }
    fTop=_imagesView.frame.origin.y;
    NSString *brand =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"brand"]] replaceNullString];
    if (brand.length>0) {
        [_brandLabel setText:brand];
        CGSize optimumSize11 = [_brandLabel optimumSize];
        CGFloat hh1 =optimumSize11.height;
        _brandLabel.frame =CGRectMake(DEVICE_WIDTH/2+PHOTO_FRAME_WIDTH, fTop, DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*2, hh1);
        fTop = fTop+hh1+PHOTO_TIME_PADDING;
    }
    
    NSString *test =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    [_titleLabel setText:test];
    CGSize optimumSize = [_titleLabel optimumSize];
    CGFloat h1 =optimumSize.height;
    _titleLabel.frame =CGRectMake(DEVICE_WIDTH/2+PHOTO_FRAME_WIDTH, fTop,DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*2, h1);
    fTop += h1+PHOTO_FRAME_WIDTH;
    //
    NSString *text =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]] replaceNullString];
    if (text.length>0) {
        text=[NSString stringWithFormat:@"<font size=17 color='#CCCCCC'><b>“ </b></font>%@<font size=17 color='#CCCCCC'><b> ”</b> </font>",text];
        [_introLabel setText:text];
        CGSize optimumSize1 = [_introLabel optimumSize];
        CGFloat h =optimumSize1.height;
        
        _introLabel.frame =CGRectMake(DEVICE_WIDTH/2+PHOTO_FRAME_WIDTH, fTop, DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*2, h);
        fTop += h+PHOTO_FRAME_WIDTH*5;
    }
    
    _priceLabel.frame=CGRectMake(DEVICE_WIDTH/2+PHOTO_FRAME_WIDTH, fTop, 150, 20);
    _priceLabel.text =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"price_str"]] replaceNullString];
    //
    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)commentAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_comment,sender,self.data,self.key,self.indexPath);
    }
}
-(void)categoryAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_category,sender,self.data,self.key,self.indexPath);
    }
}
-(void)topAction
{
    //    self.topBtn
    [UIView animateWithDuration:0.3 animations:^{
        self.topBtn.frame =CGRectMake(0, PHOTO_TIME_PADDING, PHOTO_FRAME_WIDTH*7, PHOTO_FRAME_WIDTH*7);
        self.topBtn.titleLabel.font =[UIFont systemFontOfSize:26];
    } completion:^(BOOL finished) {
        self.topBtn.frame =CGRectMake(PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING, PHOTO_FRAME_WIDTH*5, PHOTO_FRAME_WIDTH*5);
        self.topBtn.titleLabel.font =FONT(16);
    }];
    if (self.actionBlock) {
        self.actionBlock(BEventType_bangdanTop,nil,self.data,self.key,self.indexPath);
    }
}
-(void)delRankAction
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_bangdanDelDanpin,nil,self.data,self.key,self.indexPath);
    }
}
-(void)handleSingleTapFrom
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_good,nil,self.data,self.key,self.indexPath);
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary*)model;
    
    CGFloat w =[[dict objectForKey:@"largePicWidth"] floatValue];
    CGFloat h =[[dict objectForKey:@"largePicHeight"] floatValue];
    CGFloat H =0;
    if (w>0 && h>0) {
        CGFloat fullW =DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*5;
        CGFloat ratio = fullW / w;
        H = ratio * h;
    }else{
        H=DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*5;
    }
    
    H += 16 + PHOTO_TIME_PADDING*2 + (PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING)*2;
    
    CGFloat hight =PHOTO_FRAME_WIDTH ;
    NSString *brand =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"brand"]] replaceNullString];
    if (brand.length>0) {
        RTLabel *text1Label =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*2,1)];
        text1Label.font = [UIFont boldSystemFontOfSize:14];
        text1Label.lineSpacing = 4;
        text1Label.textAlignment = NSTextAlignmentLeft;
        [text1Label setText:[NSString stringWithFormat:@"%@",brand]];
        CGSize optimumSize = [text1Label optimumSize];
        text1Label=nil;
        CGFloat h1 =optimumSize.height;
        hight += h1+PHOTO_TIME_PADDING;
    }
    
    NSString *skuname =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    if (skuname.length>0) {
        RTLabel *text1Label =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*2,1)];
        text1Label.font = [UIFont boldSystemFontOfSize:14];
        text1Label.lineSpacing = 4;
        text1Label.textAlignment = NSTextAlignmentLeft;
        [text1Label setText:[NSString stringWithFormat:@"%@",skuname]];
        CGSize optimumSize = [text1Label optimumSize];
        text1Label=nil;
        CGFloat h1 =optimumSize.height;
        
        hight += h1+PHOTO_FRAME_WIDTH;
    }
    
    NSString *text =[[NSString stringWithFormat:@"%@", [model objectForKey:@"text"]] replaceNullString];
    if (text.length>0) {
        text =[NSString stringWithFormat:@"<font size=17 color='#CCCCCC'><b>“ </b></font>%@<font size=17 color='#CCCCCC'><b> ”</b> </font>",text];
        RTLabel *textLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*2,1)];
        textLabel.font = FONT(14);
        textLabel.lineSpacing = 4;
        textLabel.textAlignment = NSTextAlignmentLeft;
        [textLabel setText:text];
        CGSize optimumSize1 = [textLabel optimumSize];
        textLabel=nil;
        hight +=optimumSize1.height+PHOTO_FRAME_WIDTH*5;
    }
    hight +=MORE_BUTTON_HEIGHT;
    //  CGFloat hh = H ;
    if (hight<H) {
        return H;
    }
    return hight;
}

@end
